package com.pwt.service;

import com.pwt.common.CustomException;
import com.pwt.entity.CpConfig;
import com.pwt.repository.CpConfigRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * CpConfig service层
 *
 * @author auto generated
 * @date 2021-11-02 11:12:23
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CpConfigService {

    private CpConfigRepository cpConfigRepository;

    public CpConfigService(CpConfigRepository cpConfigRepository) {
        this.cpConfigRepository = cpConfigRepository;
    }

    public List<CpConfig> getList(){
        return this.cpConfigRepository.findAll();
    }


    public CpConfig add(CpConfig cpConfig) throws CustomException {
        CpConfig cp = this.cpConfigRepository.findCpConfigByImportanceRank(cpConfig.getImportanceRank());
        if((cpConfig.getId() == null || cpConfig.getId().trim().equals("")) && cp != null){
            throw new CustomException("该级别已配置！");
        }
        if(cp != null && (cpConfig.getId() != null && !cp.getId().equals(cpConfig.getId()))){
            throw new CustomException("该级别已配置！");
        }
        return this.cpConfigRepository.save(cpConfig);
    }


    public void  deleteCpConfig(String configId){
        this.cpConfigRepository.deleteById(configId);
    }
}
